<template>
  <el-container class="home-container">
    <el-header>
      <div>
        <img src="../assets/images/ling137.png" alt="头像" />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="primary" @click="logout">退出</el-button></el-header
    >
    <el-container>
      <el-aside :width="isCollapsed ? '64px' : '200px'">
        <div class="toggle-button" @click="toggleCollapse">
          <i v-if="!isCollapsed" class="el-icon-s-fold icon-toggle"></i>
          <i v-else class="el-icon-s-unfold icon-toggle"></i>
        </div>
        <!-- 侧边栏菜单区域 -->
        <el-menu
          :default-active="activePath"
          class="el-menu-vertical-demo"
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409eff"
          unique-opened
          :collapse="isCollapsed"
          :collapse-transition="false"
          router
        >
          <!--  unique-opened是否只允许菜单单个展开
          :collapse="isCollapsed"是否展开
          :collapse-transition="false"是否展开动画
          router路由模式是否开启 -->
          <!-- 一级菜单（可包含子菜单） -->
          <el-submenu
            v-for="item in menuList"
            :key="item.id"
            :index="item.id + ''"
          >
            <!-- 一级菜单的模板区域 -->
            <template slot="title">
              <i :class="iconObj[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单区域 -->
            <el-menu-item
              :index="'/' + subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
              @click="saveNavStatus('/' + subItem.path)"
              ><i class="el-icon-menu"></i>
              <span>{{ subItem.authName }}</span></el-menu-item
            >
          </el-submenu>
          <!-- 无子菜单 -->
          <!-- <el-menu-item>
            <i class="el-icon-menu"></i>
            <span slot="title">我的管理</span>
          </el-menu-item> -->
        </el-menu>
      </el-aside>
      <el-main>
        <router-view />
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: 'home',
  components: {},
  data() {
    return {
      menuList: [],
      iconObj: {
        125: 'el-icon-user-solid',
        103: 'el-icon-s-cooperation',
        101: 'el-icon-s-goods',
        102: 'el-icon-s-order',
        145: 'el-icon-s-marketing'
      },
      isCollapsed: false,
      activePath: ''
    }
  },
  created() {
    this.getMenuList()
    // 刷新页面哪一个子菜单是激活的则保持激活状态
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  mounted() {},
  methods: {
    logout() {
      window.sessionStorage.clear()
      this.$router.push('/login')
      this.$message.info('退出成功')
    },
    async getMenuList() {
      var result = await this.$http.get('menus')
      //   console.log(result)
      const { data: res } = result
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menuList = res.data
      //   console.log(res)
      //   this.$message.success(res.meta.msg)
    },
    // 点击按钮，实现菜单的折叠与展开
    toggleCollapse() {
      this.isCollapsed = !this.isCollapsed
    },
    // 保存子菜单高亮状态,链接的激活状态
    saveNavStatus(activePath) {
      // 将子菜单高亮状态即路由地址保存到sessionStorage中
      window.sessionStorage.setItem('activePath', activePath)
      // 每次点击子菜单更改高亮状态
      this.activePath = activePath
    }
  }
}
</script>
<style lang="less" scoped>
/* @import url(); 引入公共css类 */
.el-container {
  height: 100vh;
}
.el-header {
  //   background-color: #373f41;
  background-color: #373f41;
  display: flex;
  justify-content: space-between;
  align-items: center;
  div {
    display: flex;
    align-items: center;
    img {
      width: 40px;
      height: 40px;
      border-radius: 50%;
    }
    span {
      padding-left: 10px;
      font-family: 'Microsoft Yahei', 'Helvetica Neue', Arial, Helvetica,
        sans-serif;
      font-size: 20px;
      color: #fff;
    }
  }
}
.el-aside {
  background-color: #333744;
  .el-menu {
    border-right: 0;
  }
}

.el-main {
  background-color: #eaedf1;
}
.toggle-button {
  height: 24px;
  background-color: #4a5064;
  display: flex;
  justify-content: center;
  align-items: center;
  cursor: pointer;
  .icon-toggle {
    color: #fff;
  }
}
.el-menu-vertical-demo:not(.el-menu--collapse) {
  width: 200px;
  min-height: 400px;
}
</style>
